﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GBPVR.Public;
using System.Collections;
using System.Xml;
using System.Drawing;

namespace GBPVRLibrary.UtilityLibrary
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Disposes of a GBPVRUiElement and frees any resources used by it
        /// </summary>
        /// <param name="PluginHelper">the plugin helper that will dispose of the resource</param>
        /// <param name="UiElement">the GBPVRUiElement to dispose</param>
        /// <returns>the plugin helper that diposed of the GBPVRUiElement</returns>
        public static IPluginHelper DisposeResource(this IPluginHelper PluginHelper, GBPVRUiElement UiElement)
        {
            ArrayList array = new ArrayList();
            array.Add(UiElement);
            PluginHelper.DisposeResources(array);
            return PluginHelper;
        }

        public static SizeF getNamedImagePartPixelSize(this GBPVRX2.SkinHelper2 SkinHelper, string Name, string Part)
        {
            XmlDocument doc = SkinHelper.getBackingDocument();
            XmlNode node = doc.SelectSingleNode(String.Format("/settings/CompositeImages/CompositeImage[@name='{0}']/DrawImage[@filename='{1}']", Name, Part));
            if(node == null)
                node = doc.SelectSingleNode(String.Format("/settings/CompositeImages/CompositeImage[@name='{0}']/DrawTextWithOutline[@text='{1}']", Name, Part));
            if (node == null)
                node = doc.SelectSingleNode(String.Format("/settings/CompositeImages/CompositeImage[@name='{0}']/DrawText[@text='{1}']", Name, Part));
            if (node == null)
                return new SizeF();
            SizeF? size = ParseSize(node.Attributes["size"].Value);
            if (size == null)
                return new SizeF();
            SizeF imagePixel = SkinHelper.getNamedImagePixelSize(Name);
            return new SizeF(size.Value.Width * imagePixel.Width / 100, size.Value.Height * imagePixel.Height / 100);
        }

        public static RectangleF getNamedImageRect(this GBPVRX2.SkinHelper2 SkinHelper, string Name)
        {
            XmlDocument doc = SkinHelper.getBackingDocument();
            XmlNode node = doc.SelectSingleNode(String.Format("/settings/CompositeImages/CompositeImage[@name='{0}']", Name));
            if (node == null)
                return new RectangleF();
            RectangleF rect = new RectangleF();
            rect.Size = ParseSize(node.Attributes["size"] == null ? null : node.Attributes["size"].Value) ?? rect.Size;
            rect.Location = ParseLocation(node.Attributes["loc"] == null ? null : node.Attributes["loc"].Value) ?? rect.Location;
            return rect;
        }

        public static PointF getPlacementAttributePos(this GBPVRX2.SkinHelper2 SkinHelper, string Name, string AttributeName)
        {
            XmlNode node = SkinHelper.getPlacementNode(Name);
            if (node == null || node.Attributes[AttributeName] == null)
                return new PointF();
            return ParseLocation(node.Attributes[AttributeName].Value) ?? new PointF();
        }

        public static string getPlacementAttributeElementValue(this GBPVRX2.SkinHelper2 SkinHelper, string Name, string AttributeName)
        {
            XmlNode node = SkinHelper.getPlacementNode(Name);
            if (node == null || node.Attributes[AttributeName] == null)
                return null;
            return node.Attributes[AttributeName].Value;
        }

        public static float? getPlacementAttributeElementFloatValue(this GBPVRX2.SkinHelper2 SkinHelper, string Name, string AttributeName)
        {
            string val = getPlacementAttributeElementValue(SkinHelper, Name, AttributeName);
            float v = 0;
            if (String.IsNullOrEmpty(val) || !float.TryParse(val, out v))
                return null;
            return v;
        }

        private static SizeF? ParseSize(string Size)
        {
            if (String.IsNullOrEmpty(Size))
                return null;
            string[] parts = Size.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
                return null;
            return new SizeF(float.Parse(parts[0]), float.Parse(parts[1]));
        }
        private static PointF? ParseLocation(string Location)
        {
            if (String.IsNullOrEmpty(Location))
                return null;
            string[] parts = Location.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
                return null;
            return new PointF(float.Parse(parts[0]), float.Parse(parts[1]));
        }
    }
}
